package com.mj.flashlight.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.flashlight.R;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
 * 手电筒
 * @author zhaominglei
 * @date 2015-2-3
 * 
 */
public class MainActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = MainActivity.class.getSimpleName();
	private boolean isExit = false;
	private TimerTask timerTask;
	private FrameLayout flashlightFramelayout;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private RelativeLayout flashlightLayout;
	private ImageView switchBtn;
	private Camera camera;
	private int counter; //计数
	private Button appOffersButton; //推荐应用
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		init();
	}
	
	private void init() {
		flashlightFramelayout = (FrameLayout)findViewById(R.id.flashlight_framelayout);
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.miniAdLinearLayout);
		flashlightLayout = (RelativeLayout)findViewById(R.id.flashlight_relativelayout);
		flashlightLayout.setBackgroundColor(getResources().getColor(R.color.black));
		switchBtn = (ImageView)findViewById(R.id.flashlight_switch);
		appOffersButton = (Button)findViewById(R.id.appOffersButton);
		
		switchBtn.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		appControl = AppControl.getInstance();
		appControl.init(MainActivity.this, "6d76d763b1d3c872Fa+MZnBb/WOfXhcrrZw3irJDoSWqt8KstQd/KaoCQR5aXiNDDQ", "木蚂蚁");
		appControl.loadPopAd(MainActivity.this);
		appControl.showPopAd(MainActivity.this, 60 * 1000);
		appControl.showInter(MainActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(MainActivity.this, "6d76d763b1d3c872Fa+MZnBb/WOfXhcrrZw3irJDoSWqt8KstQd/KaoCQR5aXiNDDQ", "木蚂蚁");
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.flashlight_switch:
			//支持闪光灯 打开闪光灯 不支持闪光灯  屏幕背景变色 
			if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
				if (counter == 0) {
					Toast.makeText(MainActivity.this, R.string.flashlight_nonsupport, Toast.LENGTH_SHORT).show();
				}
			} else {
				callFlashlight();
			}
			if (counter%2 == 0) {
				flashlightFramelayout.setVisibility(View.GONE);
				flashlightLayout.setBackgroundColor(getResources().getColor(R.color.white));
				switchBtn.setImageDrawable(getResources().getDrawable(R.drawable.on));
//				AppConnect.getInstance(this).setAdBackColor(Color.WHITE);
//				AppConnect.getInstance(this).setAdForeColor(Color.BLUE);
//				AppConnect.getInstance(this).showMiniAd(this, miniAdLinearLayout, 10);
			} else {
				flashlightFramelayout.setVisibility(View.VISIBLE);
				flashlightLayout.setBackgroundColor(getResources().getColor(R.color.black));
				switchBtn.setImageDrawable(getResources().getDrawable(R.drawable.off));
//				AppConnect.getInstance(this).setAdBackColor(Color.BLACK);
//				AppConnect.getInstance(this).setAdForeColor(Color.WHITE);
//				AppConnect.getInstance(this).showMiniAd(this, miniAdLinearLayout, 10);
			}
			counter++;
			break;
		case R.id.appOffersButton:
			access.openWALL(MainActivity.this);
			break;
		default:
			break;
		}
	}

	private void callFlashlight() {
		if (camera == null) {
			camera = Camera.open();
		}
		camera.startPreview();
		Camera.Parameters parameters = camera.getParameters();
		String flashMode = parameters.getFlashMode();
		if (Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
			parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
		} else {
			parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		}
		camera.setParameters(parameters);
	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			if (camera != null) {
				camera.stopPreview();
				camera.release();
				camera = null;
			}
			MainActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MainActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}
}
